package com.swz.dcrm.adpter;

import android.content.Context;

import androidx.recyclerview.widget.RecyclerView;

import com.swz.dcrm.util.Tool;
import com.xh.baselibrary.callback.OnClickListener;



import java.util.ArrayList;
import java.util.List;

import butterknife.OnClick;

public abstract class CustomAdapter<T> extends CommonAdapter<T> {


    private int page=1;
    private int size=15;
    private int currentClickPosition;

    public OnClickListener<T> onClickListener;

    public CustomAdapter(Context context, int layoutId, List<T> datas,int size) {
        super(context, layoutId, datas);


        this.size=size;
    }

    public CustomAdapter(Context context, int layoutId, List<T> datas) {
        super(context, layoutId, datas);
    }
    public void clear(){
        mDatas.clear();
        notifyDataSetChanged();
    }

    public void refresh(int pageNum,List<T> list){

        if(pageNum==1){
            page=1;
            getDatas().clear();
            getDatas().addAll(list);
            notifyDataSetChanged();

        }else if(pageNum>page) {
            loadMore(list);
            page = pageNum;
        }


    }

    public void addItem(T t){
        if(t==null){
            return;
        }
        mDatas.add(0,t);
        notifyItemInserted(0);
        notifyDataSetChanged();
    }

    public void refreshCurrentClickItem(T t){
        if(mDatas==null||mDatas.size()==0){
            return;
        }

        mDatas.set(currentClickPosition,t);

        notifyDataSetChanged();
    }
    public void removeCurrentClickItem(){

        mDatas.remove(getDatas().get(currentClickPosition));
        notifyItemRemoved(currentClickPosition);
        notifyItemRangeChanged(currentClickPosition, mDatas.size() - currentClickPosition);
    }

    private void loadMore(List<T>list){

        if(list==null||list.size()==0){
            return;
        }
        int start=getDatas().size();

        getDatas().addAll(list);
        notifyItemChanged(start,list.size());
    }

    public void setOnClickListener(OnClickListener<T> onClickListener) {
        this.onClickListener = onClickListener;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public int getCurrentClickPosition() {
        return currentClickPosition;
    }

    public void setCurrentClickPosition(Integer currentClickPosition) {
        this.currentClickPosition = currentClickPosition;
    }

    public void smoothMoveToPosition(RecyclerView mRecyclerView, int position) {
        int firstItemPosition = -1;
        int lastItemPosition = -1;
        int left=0;
        //todo 获取第一个和最后一个可见位置方式1
        // 第一个可见位置
        firstItemPosition = mRecyclerView.getChildLayoutPosition(mRecyclerView.getChildAt(0));
        // 最后一个可见位置
        lastItemPosition = mRecyclerView.getChildLayoutPosition(mRecyclerView.getChildAt(mRecyclerView.getChildCount() - 1));

        if (position < firstItemPosition) {
            // 第一种可能:跳转位置在第一个可见位置之前
            mRecyclerView.smoothScrollToPosition(position);

        } else if (position <= lastItemPosition) {
            // 第二种可能:跳转位置在第一个可见位置之后,在最后一个可见项之前
            int movePosition = position - firstItemPosition;
            if (movePosition >= 0 && movePosition < mRecyclerView.getChildCount()) {
                 left = mRecyclerView.getChildAt(movePosition).getLeft()- Tool.dip2px(mContext,15);
                mRecyclerView.smoothScrollBy(left, 0);//dx>0===>向左  dy>0====>向上

            }

        } else {
            // 第三种可能:跳转位置在最后可见项之后
            mRecyclerView.smoothScrollToPosition(position);

        }

    }
}
